Deleting Email Attachments in Dynamics CRM 2016

 

This article could be of interest to you if you use email attachments on a large scale in CRM with images such as signatures in emails.

If you look into your database storage report and see that Email attachments is a high usage in the db size, this may be of interest to you.

 

In CRM, email attachments can be removed only if email is in draft state. You can create a workflow to change the status of the emails  and then delete the attachments manually; but it is not practical to manually delete the attachments from hundreds of emails.

 

There was a workflow solution on codeplex which was used to bulk delete the attachments using a custom workflow. 

This solution does not function in the latest version of CRM though.

 

Our engineer Subodh has created an updated version of that workflow so would work in CRM 2016.

 

This is the source code of the solution:

 

using Microsoft.Crm.Sdk;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Workflow;

using System;

using System.Activities;

using System.Collections.Generic;

using System.Linq;

using Microsoft.Xrm.Sdk.Query;

using System.Text;

namespace GWF

{

public class WorkFlow : CodeActivity

{

    [RequiredArgument]

    [Input("Email")]

    [ReferenceTarget("email")]

    public InArgument<EntityReference> rEmail { get; set;}

    protected override void Execute(CodeActivityContext executionContext)

    {

        try

        {

            ITracingService trace = executionContext.GetExtension<ITracingService>();

            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

            IOrganizationServiceFactory IOSF = executionContext.GetExtension<IOrganizationServiceFactory>();

            IOrganizationService IOS = IOSF.CreateOrganizationService(context.UserId);

            if (IOS != null)

            {

                trace.Trace("Organization Service Created");

            }

            EntityReference rem = rEmail.Get(executionContext);

            Entity REmail = IOS.Retrieve(rem.LogicalName, rem.Id, new ColumnSet("attachmentcount"));

            int atc = (int)REmail["attachmentcount"];

            try

            {

                if (atc > 0)

                {

                    QueryExpression queryAtt = new QueryExpression("activitymimeattachment");

                    queryAtt.ColumnSet = new ColumnSet(new string[] { "activityid", "attachmentid" });

                    queryAtt.Criteria = new FilterExpression();

                    queryAtt.Criteria.FilterOperator = LogicalOperator.And;

                    queryAtt.Criteria.AddCondition(new ConditionExpression("activityid", ConditionOperator.Equal, REmail.Id));

                    EntityCollection eatt = IOS.RetrieveMultiple(queryAtt);

                    for (int i = 0; i < atc; i++)

                    {

                        IOS.Delete(eatt[i].LogicalName, eatt[i].Id);

                    }

                }

}

            catch (Exception e) { throw new InvalidPluginExecutionException("Error occurred : ", e); }

        }

        catch (Exception ex)

        {

            new InvalidPluginExecutionException(String.Format("An error occurred in {0} plugin " + ex, this.GetType().ToString()));

        }

    }   

}

}

 

 

You can create the solution using this code and steps:

Build the Code.

Deploy it using PRT in any test CRM.

Create the solution in test CRM and add the workflow created by the plugin into that solution.

Export the solution as a managed solution.

 

Once the solution is imported into your CRM, these are the steps to bulk delete the attachments from emails:

 

Import the solution.

Advance find the emails with largest attachments

Select them and run the on-demand workflow which will be available after importing the solution.

You do not need to change the status of the email.

 

You can download the DeleteAttachmentsFomEmails_8_0_0_managed from here if you would like to test it.

 

Best Regards

EMEA Dynamics CRM Support Team

 

Share this Blog Article on Twitter

Tweet

Follow Us on Twitter

Follow @MSDynCRMSupport